#include"Reactor.hpp"
#include"Listener.hpp"
#include"NetCal.hpp"
#include"protocol.hpp"
int main(int argc,char* argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        exit(usageerr);
    }
    uint16_t port = std::stoi(argv[1]);
    //创建一个业务
    std::shared_ptr<NetCal> netcal=std::make_shared<NetCal>();
    //创建一个协议
    std::shared_ptr<Protocol> P=std::make_shared<Protocol>([&netcal](Request &q)->Response{
        return netcal->Handler(q);
    });
    //创建Listener
    std::shared_ptr<Connection> conn=std::make_shared<Listener>(port);
    conn->RegisterHandler([&P](std::string& inbuffer)->std::string{
        //在里边将inbuffer进行处理
        LOG(LogLevel::DEBUG)<<"进入匿名函数...";
        std::string outbuffer;
        while(true)
        {
            std::string package;
            if(!P->Decode(inbuffer,&package))
            {
                //取不出来一条完整的报文
                break;
            }
            //package为一条完整的报文,此时对package进行操作
            outbuffer+=P->Excute(package);
        }
        LOG(LogLevel::DEBUG)<<"进入结束函数...";
        std::cout<<outbuffer;
        return outbuffer;
    });
    //创建Reactor
    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();
    R->AddConnection(conn);
    R->Start();
    return 0;
}